package hczc.longcai.com.hccrowdfunding.home.second_invest.activities;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import hczc.longcai.com.hccrowdfunding.R;
import hczc.longcai.com.hccrowdfunding.activity.BaseActivity;
import hczc.longcai.com.hccrowdfunding.application.BaseApplication;
import hczc.longcai.com.hccrowdfunding.asynctask.AddressAsyncTask;
import hczc.longcai.com.hccrowdfunding.home.fourth_mine.adapters.AddressAdapter;
import hczc.longcai.com.hccrowdfunding.home.fourth_mine.beans.Address;

public class ChooseAddressActivity extends BaseActivity implements View.OnClickListener, AddressAdapter.CallBack, AddressAsyncTask.CallBack {


    private static final String TAG = "ChooseAddressActivity";

    private ListView listView;
    private List<Address> list;
    private AddressAdapter adapter;
    private TextView button;
    private Address address;

    private AddressAsyncTask asyncTask;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_address);

        initDatas();
        initViews();
    }

    private void initDatas() {
        list = new ArrayList<>();
        asyncTask = new AddressAsyncTask(this);
        asyncTask.execute(((Void) null));

//        list.add(new Address(0,"丛龙宇","13946446701","黑龙江省哈尔滨市尚志大街29号",true));
//        list.add(new Address(1,"丛龙宇","13946446701","黑龙江省哈尔滨市尚志大街29号",false));
//        list.add(new Address(2,"丛龙宇","13946446701","黑龙江省哈尔滨市尚志大街29号",false));
    }


    private void initViews() {
        findViewById(R.id.head_back).setOnClickListener(this);
        ((TextView) findViewById(R.id.head_title)).setText("选择收货地址");

        listView = ((ListView) findViewById(R.id.choose_address_listview));
        adapter = new AddressAdapter(this,list,0,this);
        listView.setAdapter(adapter);

        button = ((TextView) findViewById(R.id.choose_address_button));
        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id){
            case R.id.head_back:
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putSerializable("address",address);
                intent.putExtras(bundle);
                setResult(1,intent);
                finish();
                break;
            case R.id.choose_address_button:
                startActivityForResult(new Intent(ChooseAddressActivity.this,AddAddressActivity.class),0);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == 1){
            updateDatas();
        }
    }

    private void updateDatas() {
        list.clear();
        list.addAll(BaseApplication.dbAddressController.getAddress());
        address = list.get(0);
        adapter.notifyDataSetChanged();
    }



    @Override
    public void click(View view) {
        int id = view.getId();
        switch (id){
            case R.id.address_manage_choose:
                int tag = (int) view.getTag();
                int size = list.size();
                for(int i = 0; i < size; i ++){
                    list.get(i).setDfault(false);
                }
                list.get(tag).setDfault(true);
                address = list.get(tag);
                adapter.notifyDataSetChanged();
                break;
        }
    }



    @Override
    public void response(List<Address> info) {
        list.addAll(info);
        try {
            address = list.get(0);
        }catch (IndexOutOfBoundsException e){
            Intent intent = new Intent(ChooseAddressActivity.this, AddAddressActivity.class);
            startActivityForResult(intent,0);
        }
    }
}
